<?php $session->flash(); ?>    
<?php echo $form->create('Phramacy',array('controller'=>'pharmacies','action' => 'edit'));?>  
<?php echo $form->input('id',array('label'=>'','div'=>'','type'=>'hidden')); ?>
<table border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td valign="top">Username</td>
        <td><?php echo $form->input('Username',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Password</td>
        <td><?php echo $form->input('Password',array('type'=>'password','label'=>'','div'=>'','value'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Pharmacy name</td>
       <td><?php echo $form->input('PharmacyName',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Address</td>
        <td><?php echo $form->input('Address', array('type' => 'textarea','label'=>'','escape' => false,'rows'=>5,'cols'=>30));?></td>
    </tr>
    <tr>
        <td valign="top">County</td>
        <td>
            <select name="data[Pharmacy][Country_id]" id="UserCountry">
                <?php
                      $temp ='';
                      foreach ($GLOBALS['Counties'] as $group=>$countries)
                      {
                           if($group != $temp)
                           {
                                echo  '<optgroup label="'.$group.'">'  ;
                           }
                           foreach ($countries as $k=>$country) 
                           {
                                if($this->data['Pharmacy'][Country_id] == $k)
                                    echo  ' <option value="'.$k.'" selected="selected">'.$country.'</option>'   ;
                                else
                                    echo  ' <option value="'.$k.'">'.$country.'</option>'   ;
                           }
                           
                           $temp = $group;
                      }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td valign="top">Postcode</td>
        <td><?php echo $form->input('Postcode',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Email</td>
        <td><?php echo $form->input('Email',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
    <td valign="top">Telephone</td>
     <td><?php echo $form->input('Telephone',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Fax</td>
        <td><?php echo $form->input('Fax',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Match member since</td>
        <td><?php echo date('y-m-d',strtotime($this->data['Pharmacy']['MatchMemberSince']))?></td>
    </tr>
    <tr>
        <td valign="top">Last logged in</td>
        <td>00-00-00</td>
    </tr>
    <tr>
        <td valign="top">GPHC Number</td>
        <td><?php echo $form->input('GPHCNumber',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Date of pharmacist registration</td>
        <td><?php echo $form->input('DateOfPharmacist',array('type'=>'text','label'=>'','div'=>'')); ?></td>
    </tr>
  
    <tr>
       <td valign="top">IT compatibility</td>
       <td><?php
       if(is_array($this->data['Pharmacy']['ITCompatibility']))
       {
         $this->data['Pharmacy']['ITCompatibility'] = implode(',',$this->data['Pharmacy']['ITCompatibility']);
       }
       
       echo $form->select('ITCompatibility',$GLOBALS['ITCompatibility'],explode(',',$this->data['Pharmacy']['ITCompatibility']),array('multiple'=>true,'label'=>'','div'=>''),false);
       
       ?>
       
       </td>          
    </tr>
    <tr>
        <td valign="top">EPS1 ready</td>
         <td>Yes <?php echo $form->checkbox('EPS1Ready',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">EPS2 ready</td>
        <td valign="top">Yes <?php echo $form->checkbox('EPS2Ready',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Opening hours</td>
        <td valign="top">
        <table border="0" cellspacing="0" cellpadding="0">
        <?php foreach($GLOBALS['Days'] as $day){?>
            <tr>
            <td valign="top"><?php echo $day.':'?></td>
                <td valign="top">
                    <select name = "data[Pharmacy][<?php echo $day?>][from]">  
                         <?php foreach($GLOBALS['OpeningHours'] as $openingHours){?>
                                    <option value="<?php echo $openingHours?>"><?php echo $openingHours?></option>
                          <?php }?>
                    </select> -
                </td>
                 <td>
                     <select name = "data[Pharmacy][<?php echo $day?>][to]">   
                         <?php foreach($GLOBALS['OpeningHours'] as $openingHours){?>
                                    <option value="<?php echo $openingHours?>"><?php echo $openingHours?></option>
                          <?php }?>    
                     </select>
                 </td>
                </tr>
        <?php } ?>
        </table>
</td>
</tr>
    <tr>
        <td>Lunch closure</td>
        <td><?php echo $form->checkbox('LunchClosure',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td>Free parking</td>
        <td><?php echo $form->checkbox('FreeParking',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td>Prescription items</td>
        <td>
         <?php echo $form->select('PrescriptionItems',$GLOBALS['PrescriptionItems'],null,array('label'=>'','div'=>''),false); ?>
        </td>
    </tr>
    <tr>
        <td>Staff</td>
        <td><?php
        if(is_array($this->data['Pharmacy']['Staff']))
        {
             $this->data['Pharmacy']['Staff'] = implode(',',$this->data['Pharmacy']['Staff']);
        }
         echo $form->select('Staff',$GLOBALS['Staff'],explode(',',$this->data['Pharmacy']['Staff']),array('multiple'=>true,'label'=>'','div'=>''),false); ?></td>          
    </tr>
    <tr>
        <td>Consultation room</td>
        <td>Yes<?php echo $form->checkbox('ConsultationRoom',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td>Community dossette patients (CDS)</td>
        <td>Number <?php echo $form->input('CommunityDossettePatients',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td>Servicing of care/nursing homes</td>
        <td>Number <?php echo $form->input('Servicing',array('label'=>'','div'=>'')); ?></td>
    </tr>
    <tr>
        <td valign="top">Essential services</td>
        <td><?php
        if(is_array($this->data['Pharmacy']['EssentialServices']))
        {
            $this->data['Pharmacy']['EssentialServices'] = implode(',',$this->data['Pharmacy']['EssentialServices']);
        }
         echo $form->select('EssentialServices',$GLOBALS['EssentialServices'],explode(',',$this->data['Pharmacy']['EssentialServices']),array('label'=>'','div'=>'','multiple'=>true),false); ?></td>          
    </tr>
    <tr>
        <td valign="top">Advanced services</td>
        <td><?php 
        if(is_array($this->data['Pharmacy']['AdvancedServices']))
        {
          $this->data['Pharmacy']['AdvancedServices'] = implode(',',$this->data['Pharmacy']['AdvancedServices']);
        }
        echo $form->select('AdvancedServices',$GLOBALS['AdvancedServices'],explode(',',$this->data['Pharmacy']['AdvancedServices']),array('label'=>'','div'=>'','multiple'=>true),false); ?></td>          
    </tr>
    <tr>
        <td valign="top">Enhanced services</td>
        <td><?php 
         if(is_array($this->data['Pharmacy']['EnhancedServices']))
        {
          $this->data['Pharmacy']['EnhancedServices'] = implode(',',$this->data['Pharmacy']['EnhancedServices']);
        }
        echo $form->select('EnhancedServices',$GLOBALS['EnhancedServices'],explode(',',$this->data['Pharmacy']['EnhancedServices']),array('label'=>'','div'=>'','multiple'=>true),false); ?></td>          
    </tr>
    <tr>
        <td valign="top">Patient Group Directives</td>
        <td>
             <?php echo $form->textarea('PatientGroupDirectives',array('label'=>'','div'=>'','cols'=>30,'rows'=>5)); ?>
        </td>
    </tr>
    <tr>
        <td><?php echo $form->submit();?></td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
  </tr>
</table>

<?php echo $form->end(); ?>

<script language="javascript" type="text/javascript">
    $(document).ready(function(){
        //Edit
        $('#AjaxEditSaveForm').submit(function(){
            $.ajax({
                type: 'POST',
                url: 'pharmacies/ajax_edit_save',
                data:$(this).serializeArray(),
                dataType:'json',
                afterValidate,
                success: function(data){ 
                  
                },
                error: function(message){
                    console.log(message);
                }
                });
          return false;      
        });
        
        
        function afterValidate(data, status)  {
            $(".message").remove();
            $(".error-message").remove();
            if (data.errors) {
                onError(data.errors);
            } else if (data.success) {
                onSuccess(data.success);
            }
        }
 
        function onSuccess(data) {
            flashMessage(data.message);
            _loadingDiv.hide();
            window.setTimeout(function() {
                window.location.href = '/posts';
            }, 2000);
        };
 
        function onError(data) {
            flashMessage(data.message);
            $.each(data.data, function(model, errors) {
                for (fieldName in this) {
                    var element = $("#" + camelize(model + '_' + fieldName));
                    var _insert = $(document.createElement('div')).insertAfter(element);
                    _insert.addClass('error-message').text(this[fieldName])
                }
                _loadingDiv.hide();
            });
        };
 
        function flashMessage(message) {
            var _insert = $(document.createElement('div')).css('display', 'none');
            _insert.attr('id', 'flashMessage').addClass('message').text(message);
            _insert.insertBefore($(".posts")).fadeIn();
        }

    function camelize(string) {
        var a = string.split('_'), i;
        s = [];
        for (i=0; i<a.length; i++){
            s.push(a[i].charAt(0).toUpperCase() + a[i].substring(1));
        }
        s = s.join('');
        return s;
    }

    });
</script>